package com.hspedu.newData.dmsxl.链表.遍历;

import com.hspedu.newData.dmsxl.ds.ListNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-28 
 * @Version: 1.0
 */

// todo　重点
public class LC61旋转链表 {

    // 双指针解决
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || k == 0){
            return head;
        }

        ListNode tail = head;

        int len = 1;
        while(tail.next !=null){
            len++;
            tail = tail.next;
        }

        k = k % len;
        if (k == 0){
            return head;
        }

        ListNode newTail = head;

        for (int i = 0; i < len - k + 1; i++) {
            newTail = newTail.next;
        }

        ListNode newHead = newTail.next;

        tail.next = head;
        newTail.next = null;

        return newHead;
    }
}
